<?php

/**
 * Created by PhpStorm.
 * User: zcw 812039610@qq.com
 * Date: 2019/7/9
 * Time: 11:30
 */

namespace scripts;

use globalunit\model\AdvModel;
use globalunit\utils\GenID;
use globalunit\utils\QueueHelper;
use globalunit\utils\Tools;
use QYS\Protocol\Response;
use QYS\Protocol\Request;
use QYS\Db\Mysql;
use QYS\Db\Mongo;
use QYS\Db\Redis;
use QYS\Log\Log;
use globalunit\utils\Config;
use QYS\Core\Config  as CoreConfig;
use globalunit\model\Web3DModel;

class CMDSQL
{

    public static function createdb($request, $response, $rediscli)
    {
//         $appid = $request->getString("appid");
//         $pfid = $request->getString("pfid");
//         if (empty($appid) || empty($pfid)) {
//             return ["errcode" => 10001];
//         }
//         $model = new Web3DModel();
//         $ok = $model->get("appid = '{$appid}' and pfid = '{$pfid}'");
//         if ($ok) {
//             return ["errcode" => 0];
//         }

//         $mysql = Mysql::getInstance("mysql1");
//         $mysql->query("start transaction");
//         try {

//             $model = new Web3DModel();
//             $uuid = GenID::uuid("uuid-");
//             $suffix = md5(uniqid(microtime(true), true));
//             $model->appid = $appid;
//             $model->pfid = $pfid;
//             $model->uuid = $uuid;
//             $model->suffix = $suffix;
//             $ok = $model->insert();
//             // Log::var_dump(["sql" => $model->getLastSql()]);
//             if (!$ok) {
//                 throw new \Exception("err insert sql=" . $model->getLastSql());
//             }

//             $adv_table_name = "yyz_adv_" . $suffix;
//             $adv_child_table_name = "yyz_adv_child_" . $suffix;
//             $msg_table_name = "yyz_msgs_" . $suffix;
//             $user_table_name = "yyz_user_" . $suffix;
//             $conf_table_name = "yyz_game_config_" . $suffix;

//             $sql = <<<crifan
//                     CREATE TABLE `{$adv_table_name}` (
//                         `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
//                         `pos` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '展位',
//                         `tp` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '类型 1图片 2视频',
//                         `status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '展位状态 0关闭 1开启',
//                         `created_at` datetime DEFAULT NULL,
//                         `updated_at` datetime DEFAULT NULL,
//                         `deleted_at` datetime DEFAULT NULL,
//                         PRIMARY KEY (`id`) USING BTREE
//                         ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='广告位配置表';
// crifan;
//             $ok = $mysql->query($sql);
//             if (!$ok) {
//                 throw new \Exception("create db error sql = " . $sql);
//             }
//             $sql = <<<crifan
//                     CREATE TABLE `{$adv_child_table_name}` (
//                         `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
//                         `aid` int(10) unsigned DEFAULT '0' COMMENT '广告位ID',
//                         `url` varchar(255) DEFAULT '' COMMENT '链接地址',
//                         `created_at` datetime DEFAULT NULL COMMENT '创建时间',
//                         `updated_at` datetime DEFAULT NULL,
//                         `deleted_at` datetime DEFAULT NULL,
//                         PRIMARY KEY (`id`)
//                         ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='广告位配置子表';
// crifan;
//             $ok = $mysql->query($sql);
//             if (!$ok) {
//                 throw new \Exception("create db error sql = " . $sql);
//             }
//             $sql = <<<crifan
//                     CREATE TABLE `{$msg_table_name}` (
//                         `id` int(10) NOT NULL AUTO_INCREMENT COMMENT 'ID',
//                         `msg` varchar(50) NOT NULL DEFAULT '' COMMENT '文字',
//                         `created_at` datetime DEFAULT NULL,
//                         `updated_at` datetime DEFAULT NULL,
//                         `deleted_at` datetime DEFAULT NULL,
//                         PRIMARY KEY (`id`)
//                         ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COMMENT='聊天内容配置表';
// crifan;
//             $ok = $mysql->query($sql);
//             if (!$ok) {
//                 throw new \Exception("create db error sql = " . $sql);
//             }
//             $sql = <<<crifan
//                     CREATE TABLE `{$user_table_name}` (
//                         `id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '用户ID',
//                         `phone` varchar(20) NOT NULL DEFAULT '' COMMENT '手机号',
//                         `sex` int(1) unsigned NOT NULL DEFAULT '1' COMMENT '性别 1男 2女',
//                         `nickname` varchar(20) NOT NULL DEFAULT '' COMMENT '昵称',
//                         `avatar` varchar(255) NOT NULL DEFAULT '' COMMENT '头像',
//                         `is_login` int(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否登录过',
//                         `online_status` int(1) unsigned NOT NULL DEFAULT '0' COMMENT '在线状态 1在线 0不在线',
//                         `pfid` varchar(50) NOT NULL DEFAULT '' COMMENT '企业ID',
//                         `appid` varchar(50) NOT NULL DEFAULT '' COMMENT '作品ID',
//                         `created_at` datetime DEFAULT NULL COMMENT '创建日期',
//                         `updated_at` datetime DEFAULT NULL COMMENT '修改日期',
//                         `deleted_at` datetime DEFAULT NULL COMMENT '删除时间',
//                         PRIMARY KEY (`id`)
//                         ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表';
// crifan;
//             $ok = $mysql->query($sql);
//             if (!$ok) {
//                 throw new \Exception("create db error sql = " . $sql);
//             }
//             $sql = <<<crifan
//                     CREATE TABLE `{$conf_table_name}` (
//                         `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
//                         `room_count` int(10) unsigned NOT NULL DEFAULT '0',
//                         PRIMARY KEY (`id`)
//                         ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COMMENT='游戏配置表';
// crifan;
//             $ok = $mysql->query($sql);
//             if (!$ok) {
//                 throw new \Exception("create db error sql = ".$sql);
//             }
//             $mysql->commit();
//         } catch (\Exception $e) {
//             $mysql->rollback();
//             Log::var_dump(["Exception" => $e->getMessage()]);
//             return ['errcode' => 10009];
//         }

        return ["errcode" => 0];
    }

}

class SqlCtrl
{

    public function invoke($request, $response, $params)
    {
        $response->addHeader("Content-Type", 'application/json');
        $response->addHeader("Access-Control-Allow-Origin", "*");
        $response->addHeader("Access-Control-Allow-Methods", 'POST, GET, OPTIONS, DELETE');
        $response->addHeader('Access-Control-Allow-Headers', "Origin, X-Requested-With, Content-Type, Accept");
        $response->sendHttpHeader();
        $fname = $params['fname'];
        $rediscli = Redis::getInstance("redis1");
        $result = null;
        $result = CMDSQL::$fname($request, $response, $rediscli);
        if (isset($result['errcode']) && $result['errcode'] != 0) {
            $errcode = $result['errcode'];
            $result['errmsg'] = Config::get('errmsg', 'e' . $errcode);
        }
        Log::info('debug', $request, ['fname' => $fname, 'result' => $result]);
        $response->say(json_encode($result));
    }
}
